var t = require("../../@babel/runtime/helpers/interopRequireDefault"), e = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), i = t(require("../../@babel/runtime/regenerator")), o = require("../../@babel/runtime/helpers/asyncToGenerator"), a = require("../../utils/tool"), s = t(require("../../http/carApi")), r = t(require("../../http/directBuyApi")), n = t(require("../../behavior/directBuyBev")), u = t(require("../../http/info")), p = t(require("../../http/coupon")), d = getApp(), c = new s.default(), h = (new r.default(), 
new u.default()), l = new p.default();

Component({
    behaviors: [ n.default ],
    properties: {
        info: {
            type: Object,
            observer: function(t) {
                this.setData({
                    productId: t.productId,
                    showAgree: t.showSupportTips
                });
            }
        },
        isShow: {
            type: Boolean,
            value: !0
        }
    },
    data: {
        isSafeArea: Boolean(d.global.systemInfo.safeAreaHeight),
        isAgree: !0,
        boxNum: 1,
        maxNum: 0,
        showAgree: 0,
        active: null,
        integralStatus: null,
        couponList: []
    },
    methods: {
        getIntegral: function(t) {
            var e = t.detail.isUseIntegral;
            1 == e ? this.setData({
                integralStatus: e,
                discountType: e,
                couponHistoryId: ""
            }) : 0 == e && this.data.couponHistoryId ? this.setData({
                integralStatus: e
            }) : this.setData({
                integralStatus: e,
                discountType: e
            }), this.computedPrice();
        },
        getCouponHistoryId: function(t) {
            t.detail.couponHistoryId ? this.setData({
                discountType: 2,
                couponHistoryId: t.detail.couponHistoryId,
                integralStatus: null
            }) : this.setData({
                discountType: 0,
                couponHistoryId: ""
            }), this.computedPrice();
        },
        changeSize: function(t) {
            var e = t.currentTarget.dataset.index, i = this.properties.info.productSpecListVo[e], o = i.ipEnglishName || ("潮玩公社" == i.ipName ? "" : i.ipName);
            this.setData({
                active: e,
                sizeId: i.productSpecId,
                productId: i.productId,
                boxNum: i.boxNum,
                quantity: i.boxNum,
                sizePrice: i.memberPrice,
                discountType: 0,
                couponHistoryId: "",
                showAgree: i.showSupportTips,
                sizeTitle: o + i.productTitle,
                limitNum: i.limitNum,
                stock: i.stock,
                boughtNum: i.boughtNum
            }), this.computedPrice();
        },
        changeAgree: function(t) {
            this.setData({
                isAgree: t.detail.select
            });
        },
        plus: function() {
            if (this.data.sizeId) {
                if (this.data.quantity + this.data.boxNum > this.data.stock) return void (0, a.toastToPage)("数量超出库存上限~");
                if (this.data.limitNum && this.data.quantity + this.data.boxNum > this.data.limitNum - this.data.boughtNum) return void (0, 
                a.toastToPage)("数量超出每人限购上限~");
            } else {
                if (this.data.quantity + 1 > this.properties.info.stock) return void (0, a.toastToPage)("数量超出库存上限~");
                if (this.properties.info.limitNum && this.data.quantity + 1 > this.properties.info.limitNum - this.properties.info.boughtNum) return void (0, 
                a.toastToPage)("数量超出每人限购上限~");
            }
            this.setData({
                quantity: this.data.quantity + this.data.boxNum,
                discountType: 0,
                couponHistoryId: ""
            }), this.computedPrice();
        },
        minus: function() {
            this.data.quantity != this.data.boxNum ? (this.setData({
                quantity: this.data.quantity - this.data.boxNum,
                discountType: 0,
                couponHistoryId: ""
            }), this.computedPrice()) : (0, a.toastToPage)("数量低于范围~");
        },
        addCar: function() {
            var t = this;
            this.data.isAgree || !this.data.showAgree ? this.properties.info.productSpecListVo && this.properties.info.productSpecListVo.length > 0 && !this.data.sizeId ? (0, 
            a.toastToPage)("请先选择规格") : c.detailsAddCar({
                productId: this.data.productId,
                quantity: this.data.quantity
            }).then(function(e) {
                (0, a.toastToPage)("加入购物车成功"), t.onClose(), t.triggerEvent("refresh", {});
            }) : (0, a.toastToPage)("请先勾选协议");
        },
        startBuy: function() {
            this.properties.info.productSpecListVo && this.properties.info.productSpecListVo.length > 0 && !this.data.sizeId ? (0, 
            a.toastToPage)("请先选择规格") : this.data.isAgree || !this.data.showAgree ? this.getDefaultAddressId() : (0, 
            a.toastToPage)("请先勾选协议");
        },
        showPopup: function() {
            this.setData({
                show: !0
            }), this.properties.info.productSpecListVo && 0 == this.properties.info.productSpecListVo.length ? this.computedPrice() : this.getIntegralTotal();
        },
        onClose: function() {
            this.setData({
                show: !1
            }), this.triggerEvent("hideBuy", {});
        },
        removeDiscount: function() {
            this.onClose(), this.setData({
                discountType: 0,
                couponHistoryId: ""
            });
        },
        tappingTicket: function() {
            this.properties.info.productSpecListVo.length > 0 && !this.data.sizeId ? (0, a.toastToPage)("请先选择规格") : this.data.couponCount <= 0 || this.getCouponList();
        },
        tappingIntegral: function() {
            this.properties.info.productSpecListVo.length > 0 && !this.data.sizeId ? (0, a.toastToPage)("请先选择规格") : this.data.integralUsable <= 0 || this.selectComponent("#integral").showPopup();
        },
        getIntegralTotal: function() {
            var t = this;
            return o(i.default.mark(function e() {
                var o;
                return i.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return e.next = 2, h.getUmsMemberInfo();

                      case 2:
                        o = e.sent, t.setData({
                            integralTotal: o.data.initialIntegration
                        });

                      case 4:
                      case "end":
                        return e.stop();
                    }
                }, e);
            }))();
        },
        getCouponList: function() {
            var t = this;
            wx.showLoading({
                title: "加载中",
                mask: !0
            });
            var i = {
                productId: this.data.productId,
                quantity: this.properties.quantity
            };
            l.getCouponByGoods(i).then(function(i) {
                var o, s = e(i.data.canUseCoupon);
                try {
                    for (s.s(); !(o = s.n()).done; ) {
                        var r = o.value;
                        r.isUsable = (0, a.isLessThanNow)(r.collectStartTime), r.isExpire = (0, a.getDifferentDays)(r.collectEndTime) < 7;
                    }
                } catch (t) {
                    s.e(t);
                } finally {
                    s.f();
                }
                t.setData({
                    couponList: i.data.canUseCoupon
                }), t.selectComponent("#ticket").showPopup(), wx.hideLoading();
            }).catch(function(t) {
                wx.hideLoading();
            });
        }
    }
});